﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ocmf.Types;

namespace Ocmf.Forms
{
    public partial class EditGoToActionForm : Form
    {
        public GoTo GoTo { get; set; }
        
        public EditGoToActionForm(GoTo goTo)
        {
            InitializeComponent();
            GoTo = goTo;
            inequalityComboBox.Items.AddRange(Enum.GetNames(typeof (Inequality)));
            countersComboBox.Items.AddRange(((Experiment)GoTo.TreeView).Counters.ToArray());
            trialIndexNumericUpDown.Maximum = conditionalTrialIndexNumericUpDown.Maximum = goTo.TreeView.Nodes.Count - 1;
            trialIndexNumericUpDown.Value = conditionalTrialIndexNumericUpDown.Value = goTo.TrialIndex;

            if (goTo.IsConditional)
            {
                countersComboBox.SelectedItem = goTo.Counter;
                inequalityComboBox.SelectedItem = Enum.GetName(typeof(Inequality), goTo.Inequality);
                valueNumericUpDown.Value = goTo.ConditionValue;
                
                unconditionalRadioButton.Checked = false;
                conditionalRadioButton.Checked = true;
            }
            else
            {
                
                unconditionalRadioButton.Checked = true;
                conditionalRadioButton.Checked = false;
            }
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            GoTo.TrialIndex = Decimal.ToInt32(trialIndexNumericUpDown.Value);
            GoTo.IsConditional = false;

            if (conditionalRadioButton.Checked)
            {
                GoTo.IsConditional = true;
                GoTo.Counter = countersComboBox.SelectedItem.ToString();
                GoTo.Inequality = (Inequality)Enum.Parse(typeof (Inequality), inequalityComboBox.SelectedItem.ToString());
                GoTo.ConditionValue = Decimal.ToInt32(valueNumericUpDown.Value);
                GoTo.TrialIndex = decimal.ToInt32(conditionalTrialIndexNumericUpDown.Value);
            }

            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void unconditionalRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (conditionalRadioButton.Checked)
            {
                unconditionalGroupBox.Enabled = false;
                conditionalGroupBox.Enabled = true;
            }
            if(unconditionalRadioButton.Checked)
            {
                unconditionalGroupBox.Enabled = true;
                conditionalGroupBox.Enabled = false;
            }
        }
    }
}
